package com.oe.market.modules.ele.model.dao;


import com.google.gson.Gson;
import com.oe.market.modules.common.model.ADaoBase;
import com.oe.market.modules.ele.model.bean.RedPackage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class RedPackageDao extends ADaoBase<RedPackage> {
    public final static RedPackageDao me = new RedPackageDao();
    private static final Logger log = LoggerFactory.getLogger(RedPackageDao.class);

    public static void main(String[] args) {
        RedPackageDao request = new RedPackageDao();
        try {
            List<RedPackage> requests = request.find();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public List<RedPackage> findByGroupSn(String groupSn) {
        String sql = "select package_id, lucky_pos, current_pos, create_time from " + getTableName() + " where group_sn='" + groupSn + "';";
        try {
            return find(sql);
        } catch (SQLException e) {
            log.error(e.getMessage());
            return null;
        }
    }

    public List<RedPackage> findByRecipient(String recipient, Integer pageNumber) {
        String sql = "select package_id, lucky_pos, current_pos, create_time from " + getTableName() + " where recipient='" + recipient + "' ORDER BY oeasy_ele_red_package.create_time DESC "
                + "LIMIT " + pageNumber * 10 + "," + 10;
        try {
            return find(sql);
        } catch (SQLException e) {
            log.error(e.getMessage());
            return null;
        }
    }

    public List<RedPackage> searchUseful() {
        String sql = "select package_id, group_sn, lucky_pos, current_pos, robot_id_list, url from " + getTableName()
                + " where lucky_pos>" + 4 + " and lucky_pos > current_pos and current_pos < 11 and status = 'valid'" +
                "and lucky_pos != current_pos + 1 Limit 0,100;";
        System.out.println(sql);
        try {
            return find(sql);
        } catch (SQLException e) {
            log.error(e.getMessage());
            return null;
        }
    }

    public List<RedPackage> searchBig() {
        String sql = "select package_id, url, group_sn, lucky_pos, current_pos, robot_id_list, url from " + getTableName()
                + " where status = 'valid' and lucky_pos = current_pos + 1 and url != null Limit 0,1000;";
        System.out.println(sql);
        try {
            return find(sql);
        } catch (SQLException e) {
            log.error(e.getMessage());
            return null;
        }
    }

    public List<RedPackage> searchUseless() {
        String sql = "select package_id, group_sn, lucky_pos, current_pos, robot_id_list, url from " + getTableName()
                + " where lucky_pos>" + 5 + " and lucky_pos < current_pos and current_pos < 15  and url!=\"\" and status = 'valid' Limit 0,50;";
        System.out.println(sql);
        try {
            return find(sql);
        } catch (SQLException e) {
            log.error(e.getMessage());
            return null;
        }
    }

    public String updateStatus(String groupSn, String status) {
        String sql = "UPDATE `" + getTableName() + "` SET `status`='" + status
                +"' WHERE (`group_sn`='"+ groupSn + "');";
        return update(sql);
    }

    public String updateCurrent(String groupSn, Integer currentPos) {
        String sql = "UPDATE `" + getTableName() + "` SET `current_pos`=" + currentPos
                +" WHERE (`group_sn`='"+ groupSn + "');";
        return update(sql);
    }

    public String updateCurrentAndRobotList(String groupSn, Integer currentPos, String robotList) {
        String sql = "UPDATE `" + getTableName() + "` SET `current_pos`=" + currentPos
                + ", `robot_id_list`='" + robotList + "'"
                +" WHERE (`group_sn`='"+ groupSn + "');";
        return update(sql);
    }

    public String ban(String groupSn) {
        String sql = "UPDATE `" + getTableName() + "` SET `status`= 'invalid' WHERE (`group_sn`='" + groupSn + "');";
        return update(sql);
    }
    @Override
    public RedPackage getThisClassInstance() {
        return new RedPackage();
    }

    @Override
    public String getTableName() {
        return "oeasy_ele_red_package";
    }


}


